﻿#include "CompassDialog.h"
#include "../../Settings.h"
#include "../GameScene.h"

namespace S = ServerPackets;
using namespace Client::MirControls;
using namespace Client::MirGraphics;

namespace Client::MirScenes::Dialogs
{

    CompassDialog::CompassDialog() : _image(new MirImageControl();
        _image->setParent(this);
        _image->setIndex(0);
        _image->setLibrary(Libraries::Prguse2);
        _image->setNotControl(true);
        _image->setUseOffSet(true);
        Point tempVar(0, 0);
        _image->setLocation(&tempVar);
        _image->setVisible(true))
        {
        Point tempVar2((Settings::ScreenWidth / 2) - 25, (Settings::ScreenHeight / 2) - 120);
        setLocation(&tempVar2);
        setNotControl(true);
        Size tempVar3(10, 10);
        setSize(&tempVar3);
        setMovable(false);
        setSort(true);

        }

    void CompassDialog::ClearPoint()
    {
        Destination = Point::Empty;
    }

    void CompassDialog::SetPoint(Point *point)
    {
        Destination = point;
    }

    void CompassDialog::Process()
    {
        if (Destination == Point::Empty || (Destination->X == GameScene::getUser()->CurrentLocation->X && Destination->Y == GameScene::getUser()->CurrentLocation->Y))
        {
            setVisible(false);
            return;
        }

        setVisible(true);

        float xDiff = GameScene::getUser()->CurrentLocation->X - Destination->X;
        float yDiff = GameScene::getUser()->CurrentLocation->Y - Destination->Y;

        auto angle = std::atan2(xDiff * -1, yDiff) * 180 / M_PI;

        auto degree = (angle + 360) % 360;

        auto offset = static_cast<double>(40) / 360 * degree;

        _image->setIndex(static_cast<int>(1470 + std::floor(offset)));
    }
}
